X=imread('wed2.jpg');

%show original picture 600x600
Xoriginal=X(1:600,1:600,:);
f1=figure;
set(f, 'name', 'Original 600x600','numbertitle','off')
imagesc(Xoriginal);
axis square

r=X(:,:,1);g=X(:,:,2);b=X(:,:,3); %separate r, g, b


%Create YUV variables for Baseline JPEG method
%Y - luminance variable
%U and V - color difference variables
Y = 0.299*r + 0.587*g + 0.114*b;
U = b - Y;
V = r - Y;


QY = [16, 11, 10, 16, 24, 40, 51, 61;
        12, 12, 14, 19, 26, 58, 60, 55;
        14, 13, 16, 24, 40, 57, 69, 56;
        14, 17, 22, 29, 51, 87, 80, 62;
        18, 22, 37, 56, 68, 109, 103, 77;
        24, 35, 55, 64, 81, 104, 113, 92;
        49, 64, 78, 87, 103, 121, 120, 101;
        72, 92, 95, 98, 112, 100, 103, 99];  %use this to quantize Luminance Variable Y

QC = 50.*[
    17, 18, 24, 47, 99, 99, 99, 99;
    18, 21, 26, 66, 99, 99, 99, 99;
    24, 26, 56, 99, 99, 99, 99, 99;
    47, 66, 99, 99, 99, 99, 99, 99;
    99, 99, 99, 99, 99, 99, 99, 99;
    99, 99, 99, 99, 99, 99, 99, 99;
    99, 99, 99, 99, 99, 99, 99, 99;
    99, 99, 99, 99, 99, 99, 99, 99]; %use this to quantize color difference variables U and V

%Apply DCT filtering to Y using JPEG-suggested matrix QY
for i=1:75
    for j=1:75
        x=Y((i-1)*8+1:(i-1)*8+8,(j-1)*8+1:(j-1)*8+8); %extract 8x8 pixel block

        %Start of Compression Process
        xd=double(x); %convert uint8 to double
        xc=xd-128; %center matrix over zero
        Z=dct(dct(xc')'); %Apply the 2D-DCT, then Z is the transform matrix
        Zq=round(Z./QY); %replace Z by the compressed matrix (Quantized using QY)

        %Start of Decompression Process
        Zdq=Zq.*QY; %dequantization
        Ydq=idct2(Zdq); %inverse DCT transform
        Ye=Ydq+128; %un-center
        Yout((i-1)*8+1:(i-1)*8+8,(j-1)*8+1:(j-1)*8+8)=Ye;
    end
end

%Apply DCT filtering to color differences U and V using JPEG-suggested matrix QC
for i=1:75
    for j=1:75
        x=U((i-1)*8+1:(i-1)*8+8,(j-1)*8+1:(j-1)*8+8); %extract 8x8 pixel block

        %Start of Compression Process
        xd=double(x); %convert uint8 to double
        xc=xd-128; %center matrix over zero
        Z=dct(dct(xc')'); %Apply the 2D-DCT, then Z is the transform matrix
        Zq=round(Z./QC); %replace Z by the compressed matrix (Quantized using QC)

        %Start of Decompression Process
        Zdq=Zq.*QC; %dequantization
        Udq=idct2(Zdq); %inverse DCT transform
        Ue=Udq+128; %un-center
        Uout((i-1)*8+1:(i-1)*8+8,(j-1)*8+1:(j-1)*8+8)=Ue;
    end
end

for i=1:75
    for j=1:75
        x=V((i-1)*8+1:(i-1)*8+8,(j-1)*8+1:(j-1)*8+8); %extract 8x8 pixel block

        %Start of Compression Process
        xd=double(x); %convert uint8 to double
        xc=xd-128; %center matrix over zero
        Z=dct(dct(xc')'); %Apply the 2D-DCT, then Z is the transform matrix
        Zq=round(Z./QC); %replace Z by the compressed matrix (Quantized using QC)

        %Start of Decompression Process
        Zdq=Zq.*QC; %dequantization
        Vdq=idct2(Zdq); %inverse DCT transform
        Ve=Vdq+128; %un-center
        Vout((i-1)*8+1:(i-1)*8+8,(j-1)*8+1:(j-1)*8+8)=Ve;
    end
end

R = Vout + Yout;
B = Uout + Yout;
G =(Yout - 0.299*R - 0.114*B)/0.587;

Xout(:,:,1)=uint8(R);
Xout(:,:,2)=uint8(G);
Xout(:,:,3)=uint8(B);


f2=figure;
set(f2,'name', 'Color JPEG suggested Matrix, p=1','numbertitle','off');
imagesc(Xout); %display the image after compression
axis square;

f3=figure;
set(f3,'name', 'Comparison with p=1','numbertitle','off');
h = imshowpair(Xoriginal,Xout,'diff')
axis square;
h =

  877.0015